package com.molyfun.parents.activity.course;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.report.ReportFromsCourseAdapter;
import com.molyfun.parents.activity.report.adapter.ReportFromsTargetAdapter;
import com.molyfun.parents.rest.bean.RepoetFromsLineListResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.view.StrokeTextView;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class CourseFromsChooseActivity extends BaseActivity implements View.OnClickListener, ReportFromsTargetAdapter.ReportFromsTargetAdapterCallback, ReportFromsCourseAdapter.ReportFromsCourseAdapterCallback {

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.submit)
    StrokeTextView mSubmit;
    @InjectView(R.id.base_title)
    TextView mTitle;

    @InjectView(R.id.recyclerView_target)
    RecyclerView mTargeRecyclerView;
    @InjectView(R.id.recyclerView_course)
    RecyclerView mCourseRecyclerView;
    private ReportFromsCourseAdapter reportFromsCourseAdapter;
    private ArrayList<RepoetFromsLineListResponse.LineListBean> targetList = null;
    private String mReportId;

    private RepoetFromsLineListResponse.LineListBean.CoursenamelistBean mChoosedCoursenamelistBean;

    public static void startActivity(Context context, String title, String reportId, ArrayList<RepoetFromsLineListResponse.LineListBean> targetList) {
        Intent intent = new Intent();
        intent.setClass(context, CourseFromsChooseActivity.class);
        intent.putExtra(Const.FLAG_TITLE, title);
        intent.putExtra(Const.REPORT_ID, reportId);
        intent.putExtra("targetList", targetList);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reportfroms_choosecourse);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");
        mTitle.setText("课程选择");
        mReportId = getIntent().getStringExtra(Const.REPORT_ID);
        mBack.setOnClickListener(this);
        mSubmit.setOnClickListener(this);
        initView();

    }

    private void initView() {
        mReportId = getIntent().getStringExtra(Const.REPORT_ID);
        targetList = getIntent().getParcelableArrayListExtra("targetList");
        mTargeRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mTargeRecyclerView.setHasFixedSize(true);
        ReportFromsTargetAdapter reportFromsTargetAdapter = new ReportFromsTargetAdapter(this);
        mTargeRecyclerView.setAdapter(reportFromsTargetAdapter);
        reportFromsTargetAdapter.initData(targetList);
        mCourseRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mCourseRecyclerView.setHasFixedSize(true);
        reportFromsCourseAdapter = new ReportFromsCourseAdapter(this);
        mCourseRecyclerView.setAdapter(reportFromsCourseAdapter);
        reportFromsCourseAdapter.initData(targetList.get(0).getCoursenamelist());//默认值
        reportFromsTargetAdapter.setReportFromsTargetAdapterListener(this);
        reportFromsCourseAdapter.setReportFromsCourseAdapterListener(this);

        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)mTargeRecyclerView.getLayoutParams();
        layoutParams.width = (MFPApplication.sScreenWidth - (getResources().getDimensionPixelOffset(R.dimen.common_4)))/2;
        mTargeRecyclerView.setLayoutParams(layoutParams);

        LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams)mCourseRecyclerView.getLayoutParams();
        layoutParams.width = (MFPApplication.sScreenWidth - (getResources().getDimensionPixelOffset(R.dimen.common_4)))/2;
        mCourseRecyclerView.setLayoutParams(layoutParams2);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.submit:
                if (mChoosedCoursenamelistBean != null) {
                    MessgeBean bean = new MessgeBean();
                    bean.setTagertId(mChoosedCoursenamelistBean.getId());
                    bean.setCourserName(mChoosedCoursenamelistBean.getName());
                    bean.setTargetName(mTargetName);
                    Message uimessage = new Message();
                    uimessage.what = EventDispatcherEnum.UI_EVENT_REPORTFROMSCOURSE;
                    uimessage.obj = bean;
                    MFPApplication.getInstance().getEventDispatcher().sendMessage(uimessage);
                    finish();
                }
                break;
            default:
                break;
        }

    }

    private String mTargetName = "";

    @Override
    public void setSelectorPosition(int position, String targetName) {
        mTargetName = targetName;
        reportFromsCourseAdapter.initData(targetList.get(position).getCoursenamelist());
    }

    @Override
    public void setTagertId(RepoetFromsLineListResponse.LineListBean.CoursenamelistBean coursenamelistBean) {
        mChoosedCoursenamelistBean = coursenamelistBean;
    }

    public class MessgeBean {
        private String tagertId;
        private String courserName;
        private String targetName;


        public String getTagertId() {
            return tagertId;
        }

        public void setTagertId(String tagertId) {
            this.tagertId = tagertId;
        }

        public String getCourserName() {
            return courserName;
        }

        public void setCourserName(String courserName) {
            this.courserName = courserName;
        }

        public String getTargetName() {
            return targetName;
        }

        public void setTargetName(String targetName) {
            this.targetName = targetName;
        }
    }
}
